package meijuli.com.join51.business.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * @author Scott Smith  @Date 2016年04月16/4/22日 14:42
 */
public class PullListView extends PullViewBase {
    private VerticalListView mListView;
    private LoadMoreListView mLoadMoreListView;
    private DefaultHeaderView mHeaderView;
    private DefaultFooterView mFooterView;

    private boolean isRefreshing = false;

    private OnPullListener onPullListener;

    private int mLastMotionY = 0;
    private int mLastMotionX = 0;
    private int xDistance, yDistance;
    private boolean isCanRefresh = true;

    public interface OnPullListener {
        void onRefresh(ListView listView);

        void onLoadMore(ListView listView);
    }

    public PullListView(Context context) {
        super(context);
        initView(context);
        onFinishInflate();
    }

    public PullListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PullListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        setHeaderView(headerView());

        mLoadMoreListView = new LoadMoreListView(context, (ListView) pullView());
        DefaultFooterView footerView = (DefaultFooterView) footerView();
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLoadMoreListView.canLoadMore()) {
                    mLoadMoreListView.performLoadingMore();
                }
            }
        });
        mLoadMoreListView.setFooterView(footerView);
        addView(mLoadMoreListView);

        setLoadingMinTime(0);
        setEnabledNextPtrAtOnce(true);
        setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PullListView.this.checkCanDoRefresh(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (!isRefreshing) {
                    if (null != onPullListener) {
                        onPullListener.onRefresh((ListView) pullView());
                    }
                    mHeaderView.onUIRefreshBegin(frame);
                    mLoadMoreListView.reset();
                    isRefreshing = true;
                }
            }
        });

        addPtrUIHandler(new PtrUIHandler() {
            @Override
            public void onUIReset(PtrFrameLayout frame) {
                isRefreshing = false;
                mHeaderView.onUIReset(frame);
                mLoadMoreListView.onUIReset(frame);
            }

            @Override
            public void onUIRefreshPrepare(PtrFrameLayout frame) {
                mHeaderView.onUIRefreshPrepare(frame);
                mLoadMoreListView.onUIRefreshPrepare(frame);
            }

            @Override
            public void onUIRefreshBegin(PtrFrameLayout frame) {
                mHeaderView.onUIRefreshBegin(frame);
                mLoadMoreListView.onUIRefreshBegin(frame);
            }

            @Override
            public void onUIRefreshComplete(PtrFrameLayout frame) {
                mHeaderView.onUIRefreshComplete(frame);
                mLoadMoreListView.onUIRefreshComplete(frame);
            }

            @Override
            public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                mHeaderView.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
                mLoadMoreListView.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
            }
        });
    }

    @Override
    public View pullView() {
        if (null == mListView) {
            mListView = new VerticalListView(getContext());
        }
        return mListView;
    }

    @Override
    public View headerView() {
        if (null == mHeaderView) {
            mHeaderView = new DefaultHeaderView(getContext());
        }
        return mHeaderView;
    }

    @Override
    public View footerView() {
        if (null == mFooterView) {
            mFooterView = new DefaultFooterView(getContext());
        }
        return mFooterView;
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return !mLoadMoreListView.isLoading() && PtrDefaultHandler.checkContentCanBePulledDown(frame, pullView(), header) && isCanRefresh;
    }

    @Override
    public boolean checkCanDoLoading(PtrFrameLayout frame, View content, View header) {
        return false;
    }

    @Override
    public void onRefreshComplete() {
        refreshComplete();
        mHeaderView.onUIRefreshComplete(this);
        mLoadMoreListView.reset();
        isRefreshing = false;
    }

    @Override
    public void onLoadComplete(boolean isLast, CharSequence message) {
        mLoadMoreListView.onLoadComplete(isLast, message);
    }

    /**
     * 使用默认提示文字
     *
     * @param isLast 是否是最后一页数据
     */
    public void onLoadComplete(boolean isLast) {
        mLoadMoreListView.onLoadComplete(isLast, null);
    }

    @Override
    public void onLoadError(String error) {
        mLoadMoreListView.onLoadingError(error);
    }

    public void setOnPullListener(OnPullListener onPullListener) {
        this.onPullListener = onPullListener;
        mLoadMoreListView.setOnPullListener(onPullListener);
    }

    public void setOnScrollListener(AbsListView.OnScrollListener onScrollListener) {
        mLoadMoreListView.setOnScrollListener(onScrollListener);
    }

    public void setOnItemClientListener(AdapterView.OnItemClickListener onItemClientListener) {
        View view = mLoadMoreListView.loadMoreView();
        if (view instanceof ListView) {
            ((ListView) view).setOnItemClickListener(onItemClientListener);
        }
    }

    public void setAdapter(ListAdapter adapter) {
        mListView.setAdapter(adapter);
    }

    public ListAdapter getAdapter() {
        return mListView.getAdapter();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0;
                mLastMotionX = (int) ev.getX();
                mLastMotionY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final int CurX = (int) ev.getX();
                final int CurY = (int) ev.getY();
                xDistance += Math.abs(CurX - mLastMotionX);
                yDistance += Math.abs(CurY - mLastMotionY);
                mLastMotionX = CurX;
                mLastMotionY = CurY;
                isCanRefresh = xDistance <= yDistance;
                break;
            default:

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
